/* eslint-disable no-unused-vars */
import api from '@/api/index.js'
import {getUUID} from '@/utils/uuid_token.js'
import { post, get, From, postJson } from '@/api/request'//真实请求
const state = {
    goodInfo:{},//商品详情
    uuid_token:getUUID(),//游客临时token
}

const mutations = {
    GETGOODINFO(state, data) {
        state.goodInfo = data
    }
}

const actions = {
    getGoodInfo({ commit }, skuid) {
        return new Promise((resolve, reject) => {
            get(`${api.goodsDetail}/${skuid}`).then(res => {
                if (res.code == 200) {
                    commit('GETGOODINFO', res.data)
                    resolve(res.data)
                }
            }).catch(err => {
                reject(err)
            });
        })
    },
    //将产品添加到购物车
    addCart({ commit }, {skuId,skuNum}) {
        return new Promise((resolve, reject) => {
            post(`${api.addCart}/${skuId}/${skuNum}`,{},false).then(res => {
                if (res.code == 200) {
                    resolve(res.ok)
                }
            }).catch(err => {
                reject(err)
            });
        })
    }
}

const getters = {//简化数据
    skuInfo: state => state.goodInfo.skuInfo || {},
    spuSaleAttrList: state => state.goodInfo.spuSaleAttrList || [],
}
export default {
    state,
    mutations,
    actions,
    getters
}
